package com.mysafe.provision.service.expense;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import com.mysafe.provision.exception.ValidationError;
import com.mysafe.provision.exception.ValidationException;
import com.mysafe.provision.model.expense.Expense;
import com.mysafe.provision.persistence.expense.IExpenseDAO;
import com.mysafe.provision.service.PersistenceService;

public class ExpenseService extends PersistenceService<Expense> implements IExpenseService, Serializable {

	private static final long serialVersionUID = 6645674551695614638L;

	private IExpenseDAO expenseDAO;
	
	@Inject
	public ExpenseService(IExpenseDAO expenseDAO) {
		super(expenseDAO);
		this.expenseDAO = expenseDAO;
	}

	@Override
	public void validateInsert(Expense expense) throws ValidationException {
		List<ValidationError> errors = new ArrayList<ValidationError>();
		if (expense == null) {
			errors.add(new ValidationError(ValidationError.INVALID));
		} else {
			if (expense.getCycle() == null) {
				errors.add(new ValidationError(ValidationError.REQUIRED));
			} else {
				if (expense.getCycle().getCycleType() == null || expense.getCycle().getCycleType().getId() == null) {
					errors.add(new ValidationError(ValidationError.REQUIRED));
				}
				
				if (expense.getCycle().getIntervalType() == null || expense.getCycle().getIntervalType().getId() == null) {
					errors.add(new ValidationError(ValidationError.REQUIRED));
				}
			}
		}
		
		if (!errors.isEmpty()) {
			throw new ValidationException(errors);
		}
		
	}

	@Override
	public void validateUpdate(Expense entity) throws ValidationException {
		// TODO Auto-generated method stub
		
	}

}
